package com.mny.wan.pkg.presentation.adapter

import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.Group
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.NetworkUtils
import com.blankj.utilcode.util.StringUtils
import com.mny.mojito.entension.loadProjectPreview
import com.mny.wan.pkg.R
import com.mny.wan.pkg.data.local.UserHelper
import com.mny.wan.pkg.data.remote.model.BeanArticle
import com.mny.wan.pkg.presentation.webview.WebViewActivity
import javax.inject.Inject

class TopArticleAdapter @Inject constructor() : RecyclerView.Adapter<TopArticleViewHolder>() {
    private val mArticles = mutableListOf<BeanArticle>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopArticleViewHolder {
        return TopArticleViewHolder.create(parent)
    }

    override fun onBindViewHolder(holder: TopArticleViewHolder, position: Int) {
        holder.bind(mArticles[position])
    }

    override fun getItemCount(): Int = mArticles.size

    fun replaceTopArticles(newBanners: List<BeanArticle>) {
        mArticles.clear()
        mArticles.addAll(newBanners)
        notifyDataSetChanged()
    }

}

class TopArticleViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
    private var mGroupProject: Group = view.findViewById(R.id.groupProject)
    private var mTvUserType: TextView = view.findViewById(R.id.tvUserType)
    private var mTvPublisher: TextView = view.findViewById(R.id.tvPublisher)
    private var mTvPublishTime: TextView = view.findViewById(R.id.tvPublishTime)
    private var mTvTitle: TextView = view.findViewById(R.id.tvTitle)
    private var mTvDesc: TextView = view.findViewById(R.id.tvDesc)
    private var mTvPin: TextView = view.findViewById(R.id.tvPin)
    private var mTvChapter: TextView = view.findViewById(R.id.tvChapter)
    private var mIvProject: ImageView = view.findViewById(R.id.imgProject)
    private var mIvCollect: ImageView = view.findViewById(R.id.imgCollect)

    fun bind(item: BeanArticle?) {
        item?.apply {
            mTvTitle.text = Html.fromHtml(title)
            mTvPublishTime.text = Html.fromHtml(niceDate)
            mTvPin.visibility = View.VISIBLE
            mTvPublisher.text = if (author.isEmpty()) shareUser.trim() else author.trim()
            mTvUserType.text = if (author.isEmpty()) StringUtils.getString(R.string.wan_share_user) else StringUtils.getString(R.string.wan_author)
            mTvChapter.text = if (superChapterName.isEmpty()) "$chapterName".trim()
            else "$chapterName·$superChapterName".trim()
            if (superChapterId == 294) {
                // 开源项目主Tab
                mIvProject.loadProjectPreview(envelopePic)
                mTvTitle.text = Html.fromHtml(desc)
                mGroupProject.visibility = View.VISIBLE
            } else {
                mGroupProject.visibility = View.GONE
            }
            view.setOnClickListener {
                WebViewActivity.show(link)
            }
            mIvCollect.isSelected = item.collect
            mIvCollect.setOnClickListener {
                if (!NetworkUtils.isConnected()) {
                    return@setOnClickListener
                }
                if (!UserHelper.isLogin()) {
                    return@setOnClickListener
                }
//                mIvCollect.isSelected = if (mIsCollectArticle) !mIsCollectArticle else !item.collect
                item.collect = mIvCollect.isSelected
//                mCollectListener?.invoke(helper.adapterPosition, item, imgCollect.isSelected)
            }
        }

    }

    companion object {
        fun create(parent: ViewGroup): TopArticleViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.cell_article, parent, false)
            return TopArticleViewHolder(view)
        }
    }
}